using System.Collections.Generic;
using Engine.Core.Interfaces;
using Engine.Particles;
using Engine.Particles.Effects;
using EngineDevelopment.DependencyInjection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;

namespace EngineDevelopment
{
    public class EngineDevelopment : InjectedGame
    {
        readonly GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        public Dictionary<string, Texture2D> TexturesDict;
        private SpriteFont _outputFont;
        private int _ellapsedTimeInMilliseconds;

        [Inject]
        public IPhysicsEngine PhysicsEngine { get; set; }

        [Inject]
        public IRenderEngine RenderEngine { get; set; }

        [Inject]
        public ParticleEngine ParticleEngine { get; set; }

        public EngineDevelopment()
        {
            Kernel.Inject(this);
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            TexturesDict = new Dictionary<string, Texture2D>();
            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            TexturesDict.Add("explosion", Content.Load<Texture2D>(@"textures\particles\circle"));
            Content.Load<Effect>(@"effects\particles\lightning");
            _outputFont = Content.Load<SpriteFont>(@"fonts\debug");
        }

        protected override void Update(GameTime gameTime) {
            _ellapsedTimeInMilliseconds += gameTime.ElapsedGameTime.Milliseconds;

            var mouseLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

            var keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Home))
            {
                _graphics.ToggleFullScreen();
                _graphics.ApplyChanges();
            }
            if (keyboard.IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (CanAddNewEffect(_ellapsedTimeInMilliseconds, 100)) {
                
                if (Mouse.GetState().LeftButton == ButtonState.Pressed) {

                    //ParticleEngine.AddParticleEffect(ShotEffect.Create(TexturesDict["explosion"], mouseLocation,
                    //                                                        new Vector2(5,-2)));
                    
                    ParticleEngine.AddParticleEffect(ExplosionEffect.Create(TexturesDict["explosion"], mouseLocation,
                                                                            true));
                    _ellapsedTimeInMilliseconds = 0;
                }
            }
            
            ParticleEngine.Update(gameTime);

            PhysicsEngine.AddPhysicElements(ParticleEngine.GetPhysicElements());
            PhysicsEngine.Apply();

            base.Update(gameTime);

            if (_ellapsedTimeInMilliseconds == int.MaxValue)
                _ellapsedTimeInMilliseconds = 0;

        }

        private bool CanAddNewEffect(int ellapsedTimeInMilliseconds, int milliseconds) {
            return ellapsedTimeInMilliseconds >= milliseconds;
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            RenderEngine.AddObjectsForRendering(ParticleEngine.GetDrawables());

            RenderEngine.AddAdditionalRenderActions(
                x =>
                x.Draw(TexturesDict["explosion"],
                       new Vector2(Mouse.GetState().X - TexturesDict["explosion"].Width/2,
                                   Mouse.GetState().Y - TexturesDict["explosion"].Height/2), Color.White),
                x => x.DrawString(_outputFont, Framerate, Vector2.Zero, Color.White),
                x => x.DrawString(_outputFont, "Ellapsed Time in Milliseconds: " + _ellapsedTimeInMilliseconds.ToString(), new Vector2(0, 20), Color.White));
            RenderEngine.Render(_spriteBatch, gameTime);
            
            base.Draw(gameTime);
        }
    }
}
